{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def roc(close, n=12, fillna=False):\n",
    "    \"\"\"Rate of Change (ROC)\n",
    "\n",
    "    The Rate-of-Change (ROC) indicator, which is also referred to as simply Momentum, is a pure momentum \n",
    "    oscillator that measures the percent change in price from one period to the next. The ROC calculation \n",
    "    compares the current price with the price “n” periods ago. The plot forms an oscillator that fluctuates \n",
    "    above and below the zero line as the Rate-of-Change moves from positive to negative. As a momentum \n",
    "    oscillator, ROC signals include centerline crossovers, divergences and overbought-oversold readings. \n",
    "    Divergences fail to foreshadow reversals more often than not, so this article will forgo a detailed \n",
    "    discussion on them. Even though centerline crossovers are prone to whipsaw, especially short-term, \n",
    "    these crossovers can be used to identify the overall trend. Identifying overbought or oversold extremes \n",
    "    comes naturally to the Rate-of-Change oscillator.\n",
    "\n",
    "    https://school.stockcharts.com/doku.php?id=technical_indicators:rate_of_change_roc_and_momentum\n",
    "\n",
    "    Args:\n",
    "        close(pandas.Series): dataset 'Close' column.\n",
    "        n(int): n periods.\n",
    "        fillna(bool): if True, fill nan values.\n",
    "\n",
    "    Returns:\n",
    "        pandas.Series: New feature generated.\n",
    "\n",
    "    \"\"\"\n",
    "    roc = ((close - close.shift(n)) / close.shift(n)) * 100\n",
    "    if fillna:\n",
    "        roc = roc.replace([np.inf, -np.inf], np.nan).fillna(0)\n",
    "    return pd.Series(roc, name='roc')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "close_data = [\n",
    "    11045.27,\n",
    "    11167.32,\n",
    "    11008.61,\n",
    "    11151.83,\n",
    "    10926.77,\n",
    "    10868.12,\n",
    "    10520.32,\n",
    "    10380.43,\n",
    "    10785.14,\n",
    "    10748.26,\n",
    "    10896.91,\n",
    "    10782.95,\n",
    "    10620.16,\n",
    "    10625.83,\n",
    "    10510.95,\n",
    "    10444.37,\n",
    "    10068.01,\n",
    "    10193.39,\n",
    "    10066.57,\n",
    "    10043.75\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.DataFrame()\n",
    "df['close'] = close_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>close</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>11045.27</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>11167.32</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>11008.61</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>11151.83</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>10926.77</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>10868.12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>10520.32</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>10380.43</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>10785.14</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>10748.26</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>10896.91</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>10782.95</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>10620.16</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>10625.83</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>10510.95</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>10444.37</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>10068.01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>10193.39</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>10066.57</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>10043.75</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       close\n",
       "0   11045.27\n",
       "1   11167.32\n",
       "2   11008.61\n",
       "3   11151.83\n",
       "4   10926.77\n",
       "5   10868.12\n",
       "6   10520.32\n",
       "7   10380.43\n",
       "8   10785.14\n",
       "9   10748.26\n",
       "10  10896.91\n",
       "11  10782.95\n",
       "12  10620.16\n",
       "13  10625.83\n",
       "14  10510.95\n",
       "15  10444.37\n",
       "16  10068.01\n",
       "17  10193.39\n",
       "18  10066.57\n",
       "19  10043.75"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0          NaN\n",
       "1          NaN\n",
       "2          NaN\n",
       "3          NaN\n",
       "4          NaN\n",
       "5          NaN\n",
       "6          NaN\n",
       "7          NaN\n",
       "8          NaN\n",
       "9          NaN\n",
       "10         NaN\n",
       "11         NaN\n",
       "12   -3.848797\n",
       "13   -4.848880\n",
       "14   -4.520643\n",
       "15   -6.343892\n",
       "16   -7.859230\n",
       "17   -6.208341\n",
       "18   -4.313082\n",
       "19   -3.243411\n",
       "Name: roc, dtype: float64"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "roc(df['close'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
